<?php
/**
 * Created by coder meng.
 * User: coder meng
 * Date: 2016/6/16 23:16
 */
namespace Home\Controller;
use Home\Controller\PublicController;

header('Content-type:text/html;charset=utf8');

class CultureController extends PublicController
{
    //品牌文化
    public function index()
    {
        //---导航信息
        $this->navigation1();
        //---导航信息
        $this->display();
    }

    //真爱查询页面
    public function truelove()
    {
        //---导航信息
        $this->navigation1();
        //---导航信息

        $this->display();
    }
    //真爱见证页面
    public function news()
    {
        //---导航信息
        $this->navigation1();
        //---导航信息

        $m = M('news');
        $where['goods_id']=array('eq','0');
        $where['status']=array('not in',array('5','4'));
        $count = $m->where($where)->count();// 查询满足要求的总记录数
        //dump($count);die;
        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $Page->lastSuffix = false;//最后一页不显示为总页数
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list =$m ->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        //M('users')->join('think_news ON think_users.id=think_news.users_id')
        foreach ($list as $k => $v) {
            $a['content'] = substr($v['content'], 0, 256);
            $arr[$k] = array_merge($v, $a);
        }

//dump($list);die;
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('arr', $arr);
        $this->display();
    }
    //真爱见证文章浏览页面
    public function message()
    {
        //---导航信息
        $this->navigation1();
        //---导航信息

        $res = M('news')->where(array('users_id' => $_SESSION['user_id']))->order('addtime desc')->select();

        $this->assign('arr', $res);
        $this->display('User/message');
    }
    //真爱见证编辑文章页面
    public function newsadd()
    {//dump(I('get.id'));
        if(I('get.id')){
            $res=M('news')->find(I('get.id'));
        }
        $this->assign('arr',$res);
        $this->display('User/newsadd');
    }
    //文章操作
    public function messageAction()
    {
        $m = M('news');
        //dump(I('post.'));die;
        $content = I('post.content');
if(I('post.id')){
    $arr1['id']=I('post.id');
    $arr1['status']=4;
}
        //----屏蔽关键字
        function Filter_word($str)
        {
            $words = "java|JAVA|赌博|毒品|人口贩子|wtf|WTF|政府|中国共产党";
            $matched = preg_replace('/' . $words . '/i', '***', $str);
            return $matched;
        }

        if ($result = Filter_word($content)) {
            // var_dump($result) ;
            $arr1['content'] = $result;
        }
        //----屏蔽关键字


        $arr1['users_id'] = $_SESSION['user_id'];


        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;
        $upload->rootPath = './Public/Uploads/';// 设置附件上传根目录
        $upload->savePath = './Images/';// 设置附件上传根目录
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->saveName = ''; //上传文件的保存名称
        $upload->autoSub = false;//关闭使用子目录保存
        $info = $upload->upload($_FILES);


        if ($info) {
            //thinkPHP3.2.3多图片上传，图片命名有bug，图片无法都上传。所以保留原名，进到这里在修改保存
            $name = time() . rand(0, 9999);
            $image = new \Think\Image();
            foreach ($info as $k => $v) {
                //dump($v);
                $str = substr(strrchr($v['name'], '.'), 1);//图片格式
                //dump($str);die;
                $name = time() . rand(0, 9999);//命名
                $image->open("./Public/Uploads/Images/{$v['name']}");
                $image->thumb(300, 200)->save("./Public/Uploads/Images/" . $name . '.' . $str);//保存图，按比例缩放800*800
                $image->save("./Public/Uploads/Images/$name" . '.' . $str);//图片格式保持一致

                $image->open("./Public/Uploads/Images/{$v['name']}");
                $image->thumb(56, 56)->save("./Public/Uploads/Images_s/" . $name . '.' . $str);//保存小图，按比例缩放56*56
                $image->save("./Public/Uploads/Images_s/$name" . '.' . $str);//图片格式保持一致
                unlink("./Public/Uploads/Images/{$v['name']}");//删除原名图片
                $data["$k"] = $name . '.jpg';//图片名写入数据库

                //替换替换时，删除原来的图片 例：修改pic1的图片，此处删除原来的pic1图
                $result = $m->find(I('post.id'));
                $pic = $result["$k"];
                unlink("./Public/Uploads/Images/$pic");
                unlink("./Public/Uploads/Images_s/$pic");
            }
        }

        $arr = array_merge_recursive($data, $arr1);
        if (!$info) {//未上传图片提交
            if(I('post.id')){
                $id = $m->save($arr1);
                if ($id) {
                    $this->success('提交成功！');
                } else {
                    $this->error('提交失败!');
                }
            }else{
                $id = $m->add($arr1);
                if ($id) {
                    $this->success('提交成功！');
                } else {
                    $this->error('提交失败!');
                }
            }

        } else {//上传图片提交
            if(I('post.id')){
                $id = $m->save($arr1);
                if ($id) {
                    $this->success('提交成功！');
                } else {
                    $this->error('提交失败!');
                }
            }else{
                $id = $m->add($arr);
                if ($id) {
                    $this->success('提交成功！');
                } else {
                    $this->error('提交失败!');
                }
            }


        }


    }
    //真爱见证、新闻详情页面
    public function newsDetail()
    {
        //---导航信息
        $this->navigation1();
        //---导航信息

        $m = M('news');
        $res = $m->where(array('id' => I('get.id')))->select();
        $user=M('users')->where(array('id'=>$res[0]['users_id']))->select();
        $res[0]['user_name']=$user[0]['username'];

        //dump($res);die;
        $this->assign('arr', $res);
        $this->display();
    }
    public function newsDetele(){
        //dump(I('get.id'));
        $m=M('news');
        $res=$m->find(I('get.id'));
        unlink("./Public/Uploads/Images/" . $res['pic1']);
        unlink("./Public/Uploads/Images_s/" . $res['pic1']);
        $res=$m->delete(I('get.id'));
        if($res){
            $this->success('删除成功！');
        }else{
            $this->success('删除失败！');
        }
    }

}